package com.sixfoot.adminsys;


import com.sixfoot.adminsys.utilities.UIUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import static org.jdesktop.application.Application.launch;
import static org.springframework.util.StringUtils.isEmpty;

/**
 * 登录
 *
 * @author zhangjunjie
 */
public class LoginFrame extends JFrame{

    private static Properties props = new Properties();
    private JPanel contentPane;
    private JTextField txt_username;
    private JPasswordField txt_password;


    public LoginFrame() {
        init();
    }

    /**
     * 初始化布局
     */
    void init(){
        setTitle("HRV Manage System");
        InputStream resourceAsStream = getClass().getResourceAsStream("/com/sixfoot/adminsys/resources/images/hrv_icon.jpg");
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据
        int rc = 0;
        try {
            while ((rc = resourceAsStream.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] in_b = swapStream.toByteArray();
        ImageIcon imageIcon = new ImageIcon(in_b);
        setIconImage(imageIcon.getImage());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 377, 250);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblUsername = new JLabel("用户名：");
        lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
        lblUsername.setVerticalAlignment(SwingConstants.BOTTOM);
        lblUsername.setBounds(10, 30, 90, 15);
        contentPane.add(lblUsername);

        JLabel lblPassword = new JLabel("密码：");
        lblPassword.setVerticalAlignment(SwingConstants.BOTTOM);
        lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
        lblPassword.setBounds(10, 85, 90, 15);
        contentPane.add(lblPassword);

        txt_username = new JTextField();
        txt_username.setBounds(110, 25, 205, 25);
        contentPane.add(txt_username);
        txt_username.setColumns(10);

        txt_password = new JPasswordField();
        txt_password.setColumns(10);
        txt_password.setBounds(110, 80, 205, 25);
        contentPane.add(txt_password);

        JButton btn_login = new JButton("登录");
        btn_login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = txt_username.getText();
                String password = txt_password.getText();
                if(isEmpty(username)){
                    UIUtils.showWarning("username不能为空！");
                    txt_username.requestFocus();
                    return;

                }
                if(isEmpty(password)){
                    UIUtils.showWarning("password不能为空！");
                    txt_password.requestFocus();
                    return;
                }
                Subject user = SecurityUtils.getSubject();
                UsernamePasswordToken token = new UsernamePasswordToken(username, password);
                token.setRememberMe(true);
                try {
                    user.login(token);
                    setVisible(false);
                    String[] args = {};
                    launch(AdminSystem.class, args);
                } catch (AuthenticationException a) {
                    txt_password.setText("");
                    txt_password.requestFocus();
                    UIUtils.showWarning("用户名或密码错误！");
                    token.clear();
                }

            }
        });
        txt_username.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                    btn_login.doClick();
                }
            }
        });
        txt_password.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                    btn_login.doClick();
                }
            }
        });
        btn_login.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
        btn_login.setBounds(150, 135, 95, 25);
        contentPane.add(btn_login);
        setResizable(false);
        setLocationRelativeTo(null);
    }
}